package com.example.jcservice.service;

import com.example.jcservice.stType.Send.AppInfo;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Service
public class AppService {

    public AppInfo getAppVersion() throws IOException {
        AppInfo appInfo = new AppInfo();

        float AppVersion;

        //获取apk
        File file = new File("/www/server/nginx/html/appupdata/app");
//        File file = new File("/Users/onestep/Desktop/app");//本地测试
        String[] fileNameLists = file.list();//列出文件夹目录
//        File[] filePathLists = file.listFiles();//路径目录
        String name = fileNameLists[0];//第二个为App名字，这里有个注意   文件会排序
        AppVersion = Float.parseFloat(name.substring(name.indexOf("锦城微服务") + 5, name.indexOf(".apk")-0));
        appInfo.setVersion(AppVersion);



        //读取txt文本，拿到更新内容
        String path="/www/server/nginx/html/appupdata/txt/"+AppVersion+".txt";
        File txtFile = new File(path); // 要读取以上路径的input。txt文件
        System.out.println(file.getName());
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();
        FileInputStream in = new FileInputStream(txtFile);
        int len;
        while ((len = in.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }

        String txtContent = sb.toString();//转换成String类型
        appInfo.setUpDataContent(txtContent);

        System.out.println(txtContent);


        return appInfo;
    }


}
